package com.qingcheng.dao;

import com.qingcheng.pojo.system.Admin;
import com.qingcheng.pojo.system.Menu;
import com.qingcheng.pojo.system.Role;
import jdk.nashorn.internal.runtime.linker.LinkerCallSite;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface AdminMapper extends Mapper<Admin> {
    @Select("SELECT * FROM tb_role WHERE id IN( " +
            "SELECT role_id FROM tb_admin_role WHERE admin_id IN  " +
            "(SELECT id FROM tb_admin WHERE id =#{adminId}))")
    public List<Role> findRoleList(@Param("adminId") Integer id);

    @Select("SELECT res_key FROM tb_resource WHERE id IN  " +
            "(SELECT resource_id FROM tb_role_resource WHERE role_id IN  " +
            "(SELECT role_id FROM tb_admin_role WHERE admin_id =#{adminId}))")
    public List<String> findAllResourceById(@Param("adminId") Integer id);

    @Select("SELECT id ,NAME ,icon ,url ,parent_id parentId FROM tb_menu WHERE id IN( " +
            "SELECT menu_id FROM tb_resource_menu WHERE resource_id IN " +
            "(SELECT resource_id FROM tb_role_resource WHERE role_id IN  " +
            "(SELECT role_id FROM tb_admin_role WHERE admin_id IN( " +
            " SELECT id FROM tb_admin WHERE login_name = #{loginName} )))) " +
            "UNION  " +
            "SELECT id ,NAME ,icon ,url ,parent_id parentId FROM tb_menu WHERE id IN( " +
            "SELECT parent_id FROM tb_menu WHERE id IN( " +
            "SELECT menu_id FROM tb_resource_menu WHERE resource_id IN " +
            "(SELECT resource_id FROM tb_role_resource WHERE role_id IN " +
            "(SELECT role_id FROM tb_admin_role WHERE admin_id IN( " +
            "SELECT id FROM tb_admin WHERE login_name = #{loginName}))))) " +
            " UNION " +
            "SELECT id ,NAME ,icon ,url ,parent_id parentId FROM tb_menu WHERE id IN ( " +
            "SELECT parent_id FROM tb_menu WHERE id IN( " +
            "SELECT parent_id FROM tb_menu WHERE id IN( " +
            "SELECT menu_id FROM tb_resource_menu WHERE resource_id IN " +
            "(SELECT resource_id FROM tb_role_resource WHERE role_id IN  " +
            "(SELECT role_id FROM tb_admin_role WHERE admin_id  IN( " +
            "SELECT id FROM tb_admin WHERE login_name = #{loginName}) )))))")
    public List<Menu> findMenu(@Param("loginName") String loginName);

}
